package com.cqkjxy.controller;

import com.cqkjxy.commons.constants.CommonConstants;
import com.cqkjxy.entity.ScoringDetailEntity;
import com.cqkjxy.entity.StaffEntity;
import com.cqkjxy.entity.TechProjectEntity;
import com.cqkjxy.service.ScoringService;
import com.cqkjxy.service.StaffService;
import com.cqkjxy.service.TechProjectService;
import com.cqkjxy.utils.SetUtils;
import org.elasticsearch.common.util.set.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Controller
public class InnovProjectController {
    private static Logger log = LoggerFactory.getLogger(InnovProjectController.class);


    private final StaffService staffService;
    private final ScoringService scoringService;
    private TechProjectService techProjectService;

    @Autowired
    public InnovProjectController(StaffService staffService, ScoringService scoringService, TechProjectService techProjectService) {
        this.staffService = staffService;
        this.scoringService = scoringService;
        this.techProjectService = techProjectService;
    }

    @RequestMapping("/add_project_tech_innov")
    public String toAdd(Model model) {
        Iterable<StaffEntity> all = staffService.findAll();
        model.addAttribute("staffList", all);
        return "pages/project/project_add";
    }

    @RequestMapping("/save_project_tech_innov")
    public String save(@RequestBody TechProjectEntity project, Model model) {
        if (project.getPkId() != null) {
            TechProjectEntity one = techProjectService.findOne(project.getPkId());
            //结果集包含了所有在左集但不在右集的元素。此操作不会改变任何输入，而是返回一个全新的集合。
            //删除的主要人员
            Set<StaffEntity> mainDiffDele = Sets.difference(one.getMainStaffEntities(), project.getMainStaffEntities());
            //新增的主要人员
            Set<StaffEntity> mainDiffAdd = Sets.difference(project.getMainStaffEntities(), one.getMainStaffEntities());
            //删除的其他人员
            Set<StaffEntity> otherDiffDele = Sets.difference(one.getOtherStaffEntities(), project.getOtherStaffEntities());
            //新增的其他人员
            Set<StaffEntity> otherDiffAdd = Sets.difference(project.getOtherStaffEntities(), one.getOtherStaffEntities());
            updateDelScore(one, mainDiffDele);
            updateDelScore(one, otherDiffDele);
            if (mainDiffAdd != null) {
                one.setMainStaffEntities(mainDiffAdd);
            } else {
                one.setMainStaffEntities(null);
            }
            if (otherDiffAdd != null) {
                one.setOtherStaffEntities(otherDiffAdd);
            } else {
                one.setOtherStaffEntities(null);
            }
            Iterable<ScoringDetailEntity> resp = scoringService.save(one);
            TechProjectEntity save = techProjectService.save(project);

        } else {
            TechProjectEntity rep = techProjectService.save(project);
            Iterable<ScoringDetailEntity> resp = scoringService.save(project);
            log.info(rep.toString());
            log.info(resp.toString());
        }
        return "pages/project/project_content";
    }

    /* *
     * @description updateScore:删除人员变更后的分数
     * @author yang tao
     * @date 2021-12-23 17:33
     * @Param: one
     * @Param: DiffDele
     * @return void
     */
    private void updateDelScore(TechProjectEntity one, Set<StaffEntity> DiffDele) {
        if (DiffDele != null || DiffDele.size() != 0) {
            Iterator<StaffEntity> iterator = DiffDele.iterator();
            while (iterator.hasNext()) {
                List<ScoringDetailEntity> entities = scoringService.findOne(iterator.next().getPkId(), one.getPkId());
                deleteStaffScore(entities);
            }
        }
    }

    @RequestMapping("/project_manage_tech_innov")
    public String toContent() {
        return "pages/project/project_content";
    }

    @RequestMapping("/project_list")
    public String projectList(Model model, @RequestParam(defaultValue = "1") int page) {
        findAllStaff(model, page - 1);
        return "pages/project/project_list";
    }

    private void findAllStaff(Model model, int i) {
        Page<TechProjectEntity> list = techProjectService.findList(i, CommonConstants.PAGE_SIZE);
        model.addAttribute("projectPage", list);
    }

    @RequestMapping("/delete_project")
    public String deleteProject(Model model, int pkId) {
        List<ScoringDetailEntity> scores = scoringService.findByProject(pkId);
        deleteStaffScore(scores);
        techProjectService.delete(pkId);
        findAllStaff(model, 0);
        return "pages/project/project_list";
    }

    /* *
     * @description deleteStaffScore
     * @author yang tao
     * @date 2021-12-24 15:06
     * @Param: scores
     * @return void
     */
    private void deleteStaffScore(List<ScoringDetailEntity> scores) {
        for (int i = 0; i < scores.size(); i++) {
            StaffEntity staffEntity = staffService.findOne(scores.get(i).getStaffEntity().getPkId());
            double score = staffEntity.getStaffScore() - scores.get(i).getStaffScore();
            staffEntity.setStaffScore(score);
            staffService.save(staffEntity);
            scoringService.delete(scores.get(i).getPkId());
        }
    }

    @RequestMapping("/to_project_update")
    public String toUpdate(int pkId, Model model) {
        TechProjectEntity one = techProjectService.findOne(pkId);
        model.addAttribute("project", one);
        Iterable<StaffEntity> all = staffService.findAll();
        model.addAttribute("staffList", all);
        return "pages/project/project_update";
    }
}
